Skip to content

Conversation

aikawayataro
Copy link
Contributor

@aikawayataro aikawayataro commented Nov 19, 2024

This replaces the regex-based approach to templates with a simple non-failing and forgiving parser.

Now there are 3 types of variables:

  • Regular variable, it just expands to its value, e.g. ${FILES} (BRANCH, FILES_COUNT, FILES are supported)
  • Sliced variable, it expands to special sliced value limited by N, e.g. ${FILES:N} (currently only FILES is supported)
  • Regex variable, same as regular variable, but before expanding it replaces regex with replacement, e.g. ${BRANCH/regex/replacement}. Supports all variables that regular variables support. No newlines allowed inside regex and replacement because that's error prone. Capture groups ($1, $2, ...) are supported, as expanding directly uses Regex.Replace(String, String).

To escape variables within text, use \ before $, e.g. \${FILES}. It is also used to escape / within regex and } within replacement for regex variables. \ for escaping is only valid in those contexts, otherwise it behaves like a regular character, e.g. C:\a\b => C:\a\b, \\ => \.

I also changed the spelling of variables to be more recognizable and "standard", but legacy spellings like files, branch_name were kept for compatibility.

Adding a variable is trivial, add an entry to s_variables for regular ones and/or to s_slicedVariables for sliced ones.
Unknown variables are silently expanded to an empty string, but I propose to add some warning logic for this.

Since there is no testing framework used for SourceGit, I didn't add a test for the template engine, but I tested it externally and it works fine. I also did some manual testing inside SourceGit, but final check is needed.

Close #704

@aikawayataro
Copy link
Contributor Author

If this is merged, any thoughts on highlighting for the commit template editor?

@love-linger
Copy link
Collaborator

Since, TemplateEngine is not related to git commands and only deal with datas, I suggest you move it to the Models directory.

PS: it is better to run dotnet format once time.

@love-linger love-linger self-assigned this Nov 19, 2024
@love-linger love-linger added the enhancement New feature or request label Nov 19, 2024
@love-linger love-linger linked an issue Nov 19, 2024 that may be closed by this pull request
@aikawayataro
Copy link
Contributor Author

Ok will move it to Models. BTW I ran dotnet format on this, is there something wrong?

@love-linger
Copy link
Collaborator

image

Sometimes blank lines are used between functions, and sometimes they are not.

@aikawayataro
Copy link
Contributor Author

Oh, I group them by what they do this way. The dotnet format didn't touch this, so I thought it was normal. Not a frequent csharp programmer, will fix that 😁

@love-linger
Copy link
Collaborator

You can use

#region SOME_DESC
#endregion

to group your codes.

@love-linger love-linger merged commit 8021cd8 into sourcegit-scm:develop Nov 19, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Advanced Template for commits

2 participants